package com.shangcheng.goods.controller;


import com.github.pagehelper.PageInfo;
import com.shangcheng.goods.pojo.Sku;
import com.shangcheng.goods.pojo.Spu;
import com.shangcheng.goods.service.SkuService;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/sku")
@CrossOrigin
public class SkuController {

    @Autowired
    private SkuService skuService;

    @GetMapping
    public Map search(@RequestParam Map searchMap) {
        Map map = skuService.search(searchMap);
        return map;
    }

    @PostMapping(value = "/findList")
    public Result<List<Sku>> findList(@RequestBody(required = false) Sku sku) {
        List<Sku> skuList = skuService.findList(sku);
        return new Result<List<Sku>>(true, StatusCode.OK, "查询成功", skuList);
    }

    @GetMapping("/{id}")
    public Result<Sku> findById(@PathVariable("id") Long id) {
        Sku sku = skuService.findById(id);
        return new Result(true, StatusCode.OK, "查询成功", sku);
    }

    /**
     * @param username
     * @return
     */
    @GetMapping("/decr")
    public Result decr(@RequestParam(value = "username") String username) {
        skuService.decrCount(username);
        return new Result(true, StatusCode.OK, "扣减库存成功");
    }

}
